package edu.map.aamas.charts;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;

import edu.map.aamas.gui.colormaps.ColorMap;
import edu.map.aamas.gui.colormaps.InterpolationColorMap;
import edu.map.aamas.rnn.RNN;
import edu.map.aamas.rnn.RnnListener;

/**
 * @author ZP
 */	
public class HistogramChart extends JPanel implements RnnListener {

	private static int margin = 4;
	
	public HistogramChart(RNN rnn){
		rnn.addRnrListener(this);
		setBackground(Color.white);
	}
	
	public void rnnRestarted(RNN rnr) {
		
	}
	
	private ColorMap cmap = new InterpolationColorMap(
			new double[] {RNN.getInstance().xmin, 0, RNN.getInstance().xmax},
			new Color[] {Color.blue, Color.green, Color.red}
	);
	
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		double columnSize = (getWidth() - margin * 2) / RNN.getInstance().size;
		
		double scale = (getHeight() - margin * 2) / (RNN.getInstance().xmax - RNN.getInstance().xmin);
		
		Graphics2D g2 = (Graphics2D) g;
		g2.translate(margin, margin + scale * RNN.getInstance().xmax);
		double xbase = 0;
		xbase += (getWidth() - margin*2-((int) columnSize * RNN.getInstance().size))/2;
		
		for (int i = 0; i < RNN.getInstance().size; i++) {
			g2.setColor(Color.red);
			double val = RNN.getInstance().X[i];
			
			g2.setColor(cmap.getColor(val));
			if (val > 0)
				g2.fill(new Rectangle2D.Double(xbase, -scale * val, columnSize, scale * val));
			else
				g2.fill(new Rectangle2D.Double(xbase, 0, columnSize, -scale * val));
			
			xbase += columnSize;						
		}
		g2.setColor(Color.black);
		g2.draw(new Line2D.Double(0, 0, getWidth()-margin*2, 0));		
	}
	
	public void rnnUpdated(RNN rnr) {		
		repaint();
	}
	
}
